<?php
class cls_serve_user extends cls_serve_base
{
	function login($user_name, $password)
	{
		if(!cls_config_user::check_name_rule($user_name)){
			return return_info(false,'用户名不符合规则！');
		}else if(!cls_config_user::check_pwd_rule($password))
		{
			return return_info(false,'密码不符合规则！');
		}

		$user_data = cls_data_base::get_instance($this->name);
		$result = $user_data->check_login($user_name, $password);
		if ($result['success']) {
			$user_data->update_lastlogin($result['ret']['user_name']);
			$_SESSION['admin']['user_name'] = $result['ret']['user_name'];
			$permission_serve = parent::get_serve('permission');
			$_SESSION['admin']['permission'] = $permission_serve->get_mypermission_all($result['ret']['permission']);
			$_SESSION['admin']['login_time'] = time();
			$_SESSION['admin']['last_act_time'] = time();
			return return_info();
		} else {
			return $result;
		}
	}
	
	function logout()
	{
		session_destroy();
	}
	
	function user_info($user_name)
	{
		$user_data = parent::get_data();
		$user_info = $user_data->get_user_info($user_name);
		return $user_info;
	}
	
	function get_user_list()
	{
		$user_data = parent::get_data();
		$user_list = $user_data->get_user_list();
		return $user_list;
	}
	
	function update($parameter)
	{
		$user_data = parent::get_data();
		$result = $user_data->check_login($parameter['user_name'], $parameter['old_password']);
		if (!$result['success']) {
			return $result;
		}
		$value = array();
		
		if($parameter['password'] != $parameter['password_confirm']){
			return return_info(false, "确认密码与输入密码不一致！");
		}

		if (!empty($parameter['password']))
		{
			if(!cls_config_user::check_pwd_rule($parameter['password'])){
				return return_info(false,'新密码不符合规则！');
			} else {
				$value['password'] = md5($parameter['password']);
			}
		}
		
		if(!cls_config_user::check_name_rule($parameter['nick_name'])){
			return return_info(false,'昵称不符合规则！');
		}
		
		if(!cls_config_user::check_real_name_rule($parameter['real_name'])){
			return return_info(false,'真实姓名不符合规则！');
		}
		
		$value['nick_name'] = $parameter['nick_name'];
		$value['real_name'] = $parameter['real_name'];
		$result = $user_data->update($parameter['user_name'], $value);
		
		return $result;
	}
	
	function create($parameter)
	{
		$user_data = parent::get_data();
		$a = $user_data->get_user_info($parameter['user_name']);
		if(!empty($a)){
			return return_info(false,"用户名已存在！");
		}
		
		$value = array('user_name' => $parameter['user_name']);

		if($parameter['password'] != $parameter['password_confirm']){
			return return_info(false, "确认密码与输入密码不一致！");
		}
		
		if(!cls_config_user::check_pwd_rule($parameter['password'])){
			return return_info(false,'密码不符合规则！');
		} else {
			$value['password'] = md5($parameter['password']);
		}
				
		if(!cls_config_user::check_name_rule($parameter['nick_name'])){
			return return_info(false,'昵称不符合规则！');
		}
		
		if(!cls_config_user::check_real_name_rule($parameter['real_name'])){
			return return_info(false,'真实姓名不符合规则！');
		}
		
		$value['nick_name'] = $parameter['nick_name'];
		$value['real_name'] = $parameter['real_name'];
		$result = $user_data->create($value);
		
		return $result;
	}
}